import 'package:flutter/cupertino.dart';
import 'package:hitutor/Util/DateUtil.dart';
import 'package:hitutor/Util/MyColor.dart';
import 'package:hitutor/Util/PageUtil.dart';
import 'package:hitutor/Widget/MyImage.dart';
import 'package:hitutor/Widget/MyPage.dart';
import 'package:hitutor/data/MyCourseData.dart';

import '../../../Util/Global.dart';
import '../../../Util/HttpGo.dart';
import '../../../Widget/MyDialog.dart';
import 'ClassChange.dart';

class ClassList extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _ClassList();
  }
}

class _ClassList extends State<ClassList> {
  @override
  void initState() {
    WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
      getClassList();
    });
    super.initState();
  }

  getClassList() {
    HttpGo(context).get("/Lesson/GetCourseList?studentId=${Global.studentId}", {}, (data) {
      courseData = MyCourseData.fromJson(data);
      setState(() {});
    }, (code, error) => showToast(error));
  }

  MyCourseData? courseData;

  toChangeLesson() {
    PageUtil().open(context, ClassChange(onChangeDone: () {
      getClassList();
    }));
  }

  @override
  Widget build(BuildContext context) {
    return MyPage(
        "我的課程",
        Container(
            child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
          Container(
            padding: EdgeInsets.fromLTRB(16, 8, 0, 8),
            child: Text("異動次數 : 總共 ${courseData?.changeDateLimit} 次 / 剩餘 ${courseData?.changeDateCount} 次",
                style: TextStyle(fontSize: 13, color: MyColor.C7D, fontWeight: FontWeight.bold)),
          ),
          Expanded(
              child: ListView.builder(
                  itemCount: courseData?.courseList?.length ?? 0,
                  itemBuilder: (BuildContext context, int index) {
                    return ItemClass(courseData?.courseList?[index]);
                  }))
        ])),
        rightWidgets: [LocalImage("change.png", width: 20, padding: EdgeInsets.fromLTRB(16, 10, 16, 10), onTap: toChangeLesson)]);
  }
}

class ItemClass extends StatelessWidget {
  CourseList? data;

  ItemClass(this.data);

  @override
  Widget build(BuildContext context) {
    String endDate = getStrTime(getTimeByUtc(data?.endDate), DateUtil.YMD);
    return GestureDetector(
        child: Container(
            height: 80,
            margin: EdgeInsets.only(bottom: 8),
            padding: EdgeInsets.all(16),
            width: double.infinity,
            color: MyColor.F8,
            child: Column(mainAxisAlignment: MainAxisAlignment.spaceAround, crossAxisAlignment: CrossAxisAlignment.start, children: [
              Text("${data?.lessonName}", maxLines: 1, style: TextStyle(fontSize: 12, color: MyColor.Orange, fontWeight: FontWeight.bold)),
              Text("總堂數${data?.totalCount} | 剩餘${data?.remainingCount}堂 | 到期日：${endDate}",
                  style: TextStyle(fontSize: 12, color: MyColor.Orange, fontWeight: FontWeight.bold)),
            ])));
  }
}
